﻿; (function (window, undefined, $) {
    var Request = new Object();
    Request = GetRequest();
    $(document).ready(function () {
        var gridopt = {
            url: options.listUrl,// + "&id=" + Request["id"],
            //url: options.listUrl,
            colModel: [
                    { display: '编号', name: 'Id', width: "5%", sortable: true, hide: true, align: 'left', iskey: true },
                    { display: '项目编号', name: 'ProjectNo', width: "20%", sortable: false, align: 'left' },
                    { display: '项目名称', name: 'ProjectName', width: "20%", sortable: false, align: 'left' },
                    { display: '投资人', name: 'UserName', width: "10%", sortable: false, align: 'left' },
                    { display: '投资金额', name: 'TenderAmt', width: "10%", sortable: false, align: 'left' },
                    { display: '投资时间', name: 'CreateTime', width: "10%", sortable: false, align: 'left' },
                     {
                         display: '投资状态', name: 'Status', width: "10%", sortable: false, align: 'left', process: function (value) {
                             return value == 1 ?
                                 "<span style='color:green'>投资成功</span>"
                                 :
                                 "<span style='color:red'>投资失败</span>";
                         }
                     }
                    //{ display: '操作', name: 'Id', width: "10%", sortable: false, align: 'center', process: processOp }
            ],
            sortname: "Id",
            sortorder: "ASC",
            title: false,
            rp: 15,
            usepager: true,
            showcheckbox: false
        };
        var xjgrid = new xjGrid("gridlist", gridopt);
        function processOp(value, cell) {
            var ops = [];
            ops.push("&nbsp;<a title='编辑' href='javascript:;'  onclick=\"util.Edit('", value, "')\"><i class='fa fa-edit' ></i>编辑</a>");
            ops.push("&nbsp;&nbsp;<a title='删除' class='abtn' href='javascript:;'  onclick=\"util.Delete('", value, "','", cell[1], "')\"><i class='fa fa-trash-o' ></i>删除</a>");
            return ops.join("");
        }

        $("#formQuery").submit(function () {
            xjgrid.Query(this);
            return false;
        });
        
        $("#btnSearch").click(function () {
            $("#formQuery").submit();
        });
        
        $("#btnAdd").click(function (e) {
            var url = options.editUrl;
            location.href = url;
        });

        util.Edit = function (id) {
            var url = options.editUrl + "/" + id;
            location.href = url;
        };
        util.Delete = function (id, name) {
            if (!confirm("您确定要执行删除操作吗？")) {
                return false;
            }
            $.post(options.deleteUrl + "/" + id, { id: id },
                  function (res) {
                      if (res.Status > 0) {
                          alert("操作成功！");
                          xjgrid.Reload();
                          return;
                      }
                      else {
                          alert("操作失败！");
                          return;
                      }
                  },
                  "json"
            );
        };
        //流标
        $("#btnLiuBiao").click(function () {
            var projectid = Request["id"];
            $(this).attr("disabled", true);
            if (!confirm("您确定要执行删除操作吗？")) {
                $(this).attr("disabled", false);
                return;
            }
            //$.post(options.liuBIaoUrl + "/", { ProjectId: projectid },
            //      function (rMsg) {
            //          alert(rMsg.Status);
            //          alert(rMsg.Message);
            //          alert(rMsg.Data);
            //          //var obj = JSON.parse(data);
            //          if (rMsg.Status > 0) {
            //              alert("操作成功！");
            //              $("#btnLiuBiao").attr("disabled", false);
            //              //xjgrid.Reload();
            //              return;
            //          }
            //          else {
            //              alert("操作失败！");
            //              return;
            //          }
            //      },
            //      "json"
            //);
            
            $.ajax({
                url: options.liuBIaoUrl,
                type: "Post",
                success: function (data) {
                    var obj = JSON.parse(data);
                    if (obj.errorCode == 200) {
                        alert(obj.errorMessage);
                    } else {
                        alert(obj.errorMessage);
                    }
                    $("#btnLiuBiao").attr("disabled", false);
                    return;
                },
                data: "projectid=" + projectid//这里没有参数可以不穿
            });
        });
    });
})(window, undefined, jQuery);

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}